import { API } from "@/services";
import { ref } from "vue";
import { defineStore } from "pinia";
import { Role } from "@/services/users/types";
export interface UserItem {
  id: number
  username: string
  role: Role
}

export const useUserListStore = defineStore("userList", () => {
  const userList = ref<UserItem[]>()
  async function dispatchGetAllUser() {
    await API.users.getAllUser().then((res) => {
      if (res.status === 200) {
        console.log(res.data);
        const student = res.data.students.map((item: {S_id:number, name:string}) => {
          return {
            id: item.S_id,
            username: item.name,
            role: Role.Student,
          };
        });
        const teacher = res.data.teachers.map((item: {T_id:number, name:string}) => {
          return {
            id: item.T_id,
            username: item.name,
            role: Role.Teacher,
          }
        });
        userList.value = student.concat(teacher);
        console.log(userList.value);
      }
    });
  }

  return {
    userList,
    dispatchGetAllUser,
  }
});
